package com.rongc.client.freight.base.view.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.flyco.animation.ZoomEnter.ZoomInEnter;
import com.flyco.animation.ZoomExit.ZoomOutExit;
import com.flyco.dialog.listener.OnBtnClickL;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.BaseFragment;
import com.rongc.client.freight.base.model.BaseDic;
import com.rongc.client.freight.base.request.api.BannerApi;
import com.rongc.client.freight.base.request.result.BannerResult;
import com.rongc.client.freight.base.view.adapter.RecyclerBaseAdapter;
import com.rongc.client.freight.base.view.adapter.RecyclerMainAdapter;
import com.rongc.client.freight.base.view.widget.AutoBanner;
import com.rongc.client.freight.base.view.widget.DividerItemDecoration;
import com.rongc.client.freight.base.view.widget.IDDialog;
import com.rongc.client.freight.business.carrier.model.ConveyanceDic;
import com.rongc.client.freight.business.carrier.view.activity.ConveyanceCreateActivity;
import com.rongc.client.freight.business.carrier.view.activity.ConveyanceSupplyListActivity;
import com.rongc.client.freight.business.supply.model.SupplyBean;
import com.rongc.client.freight.business.supply.model.SupplyDic;
import com.rongc.client.freight.business.supply.model.SupplyMyList;
import com.rongc.client.freight.business.supply.request.api.SupplyMyListApi;
import com.rongc.client.freight.business.supply.view.activity.SupplyConveyanceListActivity;
import com.rongc.client.freight.business.supply.view.activity.SupplyCreateActivity;
import com.rongc.client.freight.business.supply.view.activity.SupplyWaybillListActivity;
import com.rongc.client.freight.business.supply.view.adapter.RecyclerSupplyListAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Bruce on 2017/06/29.
 */
public class HomeFragment extends BaseFragment implements View.OnClickListener{
    @Bind(R.id.auto_banner)
    AutoBanner autoBanner;
    @Bind(R.id.recycler)
    RecyclerView recyclerView;
    @Bind(R.id.recycler_list)
    RecyclerView mRvBot;

    RecyclerSupplyListAdapter mLvAdapter;
    RecyclerMainAdapter mRecyclerAdapter;

    List<HashMap<String,String>> lsOperate;

    List<BannerResult> lsBanners;
    List<SupplyBean> lsNews;
    IDDialog dialog;

    public static HomeFragment getInstance(){
        /*if(fragment == null) fragment = new HomeFragment(orderType);
        return fragment;*/
        HomeFragment fragment = new HomeFragment();
        Bundle bdl = new Bundle();
//        bdl.putString("orderType", orderType);
        fragment.setArguments(bdl);
        return fragment;
    }

    @OnClick({R.id.toolbar_title,R.id.tv_more})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.toolbar_title:

                dialog.contentTextSize(14)
                        .contentTextColor(Color.parseColor("#444444"))
                        .contentGravity(Gravity.CENTER)
                        .showAnim(new ZoomInEnter())
                        .dismissAnim(new ZoomOutExit())
                        .cornerRadius(8)
                        .show();

                dialog.show();
                break;
            case R.id.tv_more:
                ActivityUtils.startActivity(getActivity(),SupplyWaybillListActivity.class);
                break;

        }
    }

    Response.Listener<SupplyMyList> respNewsListener = new Response.Listener<SupplyMyList>() {

        @Override
        public void onResponse(SupplyMyList response) {
            if (HandleCode.requestSuccess()) {
                if(response != null && response.getList() != null){
                    lsNews.clear();
                    lsNews.addAll(response.getList());
                    mLvAdapter.notifyDataSetChanged();
                }


//                mLvAdapter.notifyDataSetChanged();
            }
        }
    };

    Response.Listener<BannerResult[]> respBannerListener = new Response.Listener<BannerResult[]>() {

        @Override
        public void onResponse(BannerResult[] response) {
            if (HandleCode.requestSuccess()) {
                if (response != null && response.length != 0) {
                    lsBanners.clear();
                }
                List<String> imgesUrl = new ArrayList<>();
                for (int i = 0; i < response.length; i++) {
                    lsBanners.add(response[i]);
                }
                for (BannerResult r: lsBanners) {
                    imgesUrl.add(/*ApiUrl.getDefaultHost() + */r.getUrl());
                }
                autoBanner.setImagesUrl(imgesUrl);
                autoBanner.setOnItemClickListener(new AutoBanner.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        BannerResult homeBannerResult = lsBanners.get(position);
                        ActivityUtils.jumpBrowser(getActivity(),homeBannerResult.getJumpUrl());
                    }
                });

//                mVpAdapter.notifyDataSetChanged();
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
        }
    };

    @Override
    public void initListener() {

        mRecyclerAdapter.setOnItemClickListener(new RecyclerBaseAdapter.OnItemClickListener<HashMap<String, String>>() {
            @Override
            public void onItemClick(View view, int position, HashMap<String, String> data) {
//                ActivityUtils.startActivity(getActivity(),AuthActivity.class);

                switch (data.get("type")){
                    case BaseDic.OperateType.SHIPPER_CREATE_TYPE:
                        ActivityUtils.startActivity(getActivity(),SupplyCreateActivity.class);
                        break;
                    case BaseDic.OperateType.SHIPPER_FIND_TYPE:
                        ActivityUtils.startActivity(getActivity(),SupplyConveyanceListActivity.class);
                        break;
                    case BaseDic.OperateType.CARRIER_CREATE_TYPE:
                        ActivityUtils.startActivity(getActivity(),ConveyanceCreateActivity.class);
                        break;
                    case BaseDic.OperateType.CARRIER_FIND_TYPE:
                        ActivityUtils.startActivity(getActivity(),ConveyanceSupplyListActivity.class);
                        break;
                }
            }
        });
    }

    @Override
    public void initData() {
        RequestManager.getInstance().call(new SupplyMyListApi(new SupplyMyListApi.SupplyMyListParams(
                UniApplication.getInstance().getUserInfo().getUserId(),"1"),
                respNewsListener, errorListener));

        RequestManager.getInstance().call(new BannerApi(new BannerApi.BannerParams(),
                respBannerListener, errorListener));

        lsOperate.clear();
        switch (UniApplication.getInstance().getType()){
            case BaseDic.IdentityType.DRIVING_TYPE:
                initToolbar(R.string.main_title_carrier);
                HashMap<String, String> operate = new HashMap<String, String>();
                operate.put("type",BaseDic.OperateType.CARRIER_CREATE_TYPE);
                lsOperate.add(operate);
                operate = new HashMap<String, String>();
                operate.put("type",BaseDic.OperateType.CARRIER_FIND_TYPE);
                lsOperate.add(operate);
                break;
            case BaseDic.IdentityType.OWNER_TYPE:
                initToolbar(R.string.main_title_shipper);
                operate = new HashMap<String, String>();
                operate.put("type",BaseDic.OperateType.SHIPPER_CREATE_TYPE);
                lsOperate.add(operate);
                operate = new HashMap<String, String>();
                operate.put("type",BaseDic.OperateType.SHIPPER_FIND_TYPE);
                lsOperate.add(operate);
                break;
        }
        mLvAdapter.notifyDataSetChanged();
        mRecyclerAdapter.notifyDataSetChanged();
    }

    @Override
    public void initView() {

        lsNews = new ArrayList<>();
        lsBanners = new ArrayList<>();
        lsOperate = new ArrayList<>();

        mLvAdapter = new RecyclerSupplyListAdapter(getActivity(), lsNews);
        mRvBot.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
        mRvBot.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        mRvBot.setAdapter(mLvAdapter);

        mRecyclerAdapter = new RecyclerMainAdapter(getActivity(),lsOperate);

        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        recyclerView.setAdapter(mRecyclerAdapter);

        dialog = new IDDialog(getActivity());
        dialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                UniApplication.getInstance().getUserInfo().setType(BaseDic.IdentityType.OWNER_TYPE);
                UniApplication.getInstance().getUserInfo().save();
                initData();
                dialog.dismiss();
            }
        }, new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                UniApplication.getInstance().getUserInfo().setType(BaseDic.IdentityType.DRIVING_TYPE);
                UniApplication.getInstance().getUserInfo().save();
                initData();
                dialog.dismiss();
            }
        });

        mLvAdapter.setOnItemClickListener(new RecyclerBaseAdapter.OnItemClickListener<SupplyBean>() {
            @Override
            public void onItemClick(View view, int position, SupplyBean data) {
                switch (UniApplication.getInstance().getType()) {
                    case BaseDic.IdentityType.DRIVING_TYPE:
                        ConveyanceDic.jumpActivity(getActivity(),position,data);
                        break;
                    case BaseDic.IdentityType.OWNER_TYPE:
                        SupplyDic.jumpActivity(getActivity(),position,data);
                        break;
                }
            }
        });
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_host_home;
    }
}
